﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace School.UserCentents
{
    public partial class Class : UserCheckPower
    {
        #region 分页相关

        //获取传入当前页码
        int PageIndex
        {
            get
            {
                if (Request.QueryString.Get("page") != null)
                {
                    if (!Common.TypeValidator.IsNumeric(Request.QueryString.Get("page")))
                    {
                        return 1;
                    }
                    return Convert.ToInt32(Request.QueryString.Get("page"));
                }
                return 1;
            }
        }

        //分页数
        private int PageSize
        {
            get
            {
                return 10;
            }
        }
        #endregion
        BLL.Users Users = new BLL.Users();
        BLL.Classes Classes = new BLL.Classes();
        BLL.School Sch = new BLL.School();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (new BLL.Users().GetModel(LoginUserId).UserTypeID <= 3)
            {
                Panel p1 = (Panel)Master.FindControl("Panel1");
                Panel p2 = (Panel)Master.FindControl("Panel2");
                Panel p3 = (Panel)Master.FindControl("Panel3");
                Panel p4 = (Panel)Master.FindControl("Panel4");
                p1.CssClass = "sldmenuoff";
                p2.CssClass = "sldmenuon0";
                p3.CssClass = "sld1";
                p4.CssClass = "sld0";
                p3.Attributes.Add("style", "display: none");
                p4.Attributes.Add("style", "");
            }
            if (!IsPostBack)
            {
                PageLoad();
            }
        }

        private void PageLoad()
        {
            string strWhere = " and CreateUserID=" + LoginUserId;
            DataSet ds = Classes.GetList("CreateUserID=" + LoginUserId);
            Model.Users u = new BLL.Users().GetModel(LoginUserId);
            if (u.UserTypeID == 1)
            {
                XueXiaoClass.Visible = true;
                DropDownList1.DataSource = Sch.GetAllList();
                DropDownList1.DataValueField = "ID";
                DropDownList1.DataTextField = "Name";
                DropDownList1.DataBind();
                DropDownList1.Items.Insert(0, new ListItem("请选择", "0"));
                strWhere = "";
                ds = Classes.GetAllList();
            }
            else if (u.UserTypeID == 2)
            {
                XueXiaoClass.Visible = true;
                DropDownList1.DataSource = Sch.GetList("CreateUserID=" + LoginUserId);
                DropDownList1.DataValueField = "ID";
                DropDownList1.DataTextField = "Name";
                DropDownList1.DataBind();
                DropDownList1.Items.Insert(0, new ListItem("请选择", "0"));

                strWhere = " and Remark1=" + LoginUserId;
                ds = Classes.GetList("Remark1=" + LoginUserId);
            }
            else if (u.UserTypeID == 3)
            {
                ds = Users.GetList("CreateUserID=" + LoginUserId);
                string s = LoginUserId + ",";
                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        s += ds.Tables[0].Rows[i]["ID"].ToString() + ",";
                    }
                }
                strWhere = " and CreateUserID in (" + s.TrimEnd(',') + ")";
                ds = Classes.GetList("CreateUserID in (" + s.TrimEnd(',') + ") ");
            }
            else if (u.UserTypeID == 4)
            {
                DataSet ds2 = new BLL.ClassPersonnel().GetList("Manager=" + LoginUserId);
                string s = "";
                if (ds2.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
                    {
                        s += ds2.Tables[0].Rows[i]["ClassID"].ToString() + ",";
                    }
                    strWhere += " or ID in (" + s.TrimEnd(',') + ")";
                    ds = Classes.GetList("CreateUserID=" + LoginUserId + " or ID in (" + s.TrimEnd(',') + ") ");
                }
            }

            if (ds.Tables[0].Rows.Count > 0)
            {
                Repeater1.DataSource = Classes.GetList(PageSize, PageIndex, strWhere);
                Repeater1.DataBind();
                AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;
                AspNetPager1.PageSize = 10;
                AspNetPager1.CurrentPageIndex = PageIndex;
                AspNetPager1.DataBind();
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedValue != "0")
            {
                string strWhere = " and SchoolId=" + DropDownList1.SelectedValue;
                DataSet ds = Classes.GetList("SchoolId=" + DropDownList1.SelectedValue);
                Repeater1.DataSource = Classes.GetList(PageSize, PageIndex, strWhere);
                Repeater1.DataBind();
                AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;
                AspNetPager1.PageSize = 10;
                AspNetPager1.CurrentPageIndex = PageIndex;
                AspNetPager1.DataBind();
            }
            else
            {
                PageLoad();
            }
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label lbID = (Label)e.Item.FindControl("lbID");
                Label lb2 = (Label)e.Item.FindControl("Label2");

                string a = "";
                DataSet ds = new BLL.ClassPersonnel().GetList("ClassID=" + lbID.Text);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        if (ds.Tables[0].Rows[i]["Manager"].ToString() != "0")
                        {
                            a += Users.GetModel(int.Parse(ds.Tables[0].Rows[i]["Manager"].ToString())).MemberName + " | ";
                        }
                    }
                }
                char[] MyChar = { ' ', '|' };
                lbID.Text = a.TrimEnd(MyChar);
                lb2.Text = Sch.GetModel(int.Parse(lb2.Text)).Name;
            }
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                Response.Redirect("AddClass.aspx?id=" + e.CommandArgument);
            }
            if (e.CommandName == "Group")
            {
                Response.Redirect("GroupClass.aspx?id=" + e.CommandArgument);
            }
            if (e.CommandName == "Del")
            {
                Classes.Delete(int.Parse(e.CommandArgument.ToString()));
                PageLoad();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Model.Users u = new BLL.Users().GetModel(LoginUserId);
            string strWhere = " and Name like '%" + keyword.Text.Trim() + "%' and CreateUserID=" + LoginUserId;
            DataSet ds = Classes.GetList("Name like '%" + keyword.Text.Trim() + "%' and CreateUserID=" + LoginUserId);
            if (u.UserTypeID == 1)
            {
                strWhere = " and Name like '%" + keyword.Text.Trim() + "%'";
                ds = Classes.GetList("Name like '%" + keyword.Text.Trim() + "%'");
            }
            else if (u.UserTypeID == 4)
            {
                DataSet ds2 = new BLL.ClassPersonnel().GetList("Manager=" + LoginUserId);
                string s = "";
                if (ds2.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
                    {
                        s += ds2.Tables[0].Rows[i]["ClassID"].ToString() + ",";
                    }
                    strWhere += " or ID in (" + s.TrimEnd(',') + ")";
                    ds = Classes.GetList("CreateUserID=" + LoginUserId + " or ID in (" + s.TrimEnd(',') + ") ");
                }
            }

            Repeater1.DataSource = Classes.GetList(PageSize, PageIndex, strWhere);
            Repeater1.DataBind();
            AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;
            AspNetPager1.PageSize = 10;
            AspNetPager1.CurrentPageIndex = PageIndex;
            AspNetPager1.DataBind();
        }
    }
}